package com.briup.es.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.es.domain.bean.Category;
import com.briup.es.domain.vo.CategoryVO;
import com.briup.es.service.CategoryService;
import com.briup.es.mapper.CategoryMapper;
import com.briup.es.util.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    private final CategoryMapper categoryMapper;

    @Override
    public List<CategoryVO> queryAll() {
        return categoryMapper.queryAllWithChildren();
    }

    private List<CategoryVO> queryAll2(){
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<Category>()
                .isNull(Category::getParentId);
        // 查询所有的父栏目
        List<Category> categories = baseMapper.selectList(wrapper);
        // 遍历父栏目
        return categories.stream()
                .map(category -> {
                    CategoryVO categoryVO = BeanUtil.bean2VO(category, CategoryVO.class);
                    LambdaUpdateWrapper<Category> wrapper2 = new LambdaUpdateWrapper<Category>()
                            .eq(Category::getParentId, category.getParentId());
                    List<CategoryVO> children = BeanUtil.bean2VOS(
                            baseMapper.selectList(wrapper2), CategoryVO.class);
                    categoryVO.setChildren(children);
                    return categoryVO;
                }).toList();
    }
}




